﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using Editor.Utilities;
using Editor.PageControls;

namespace Editor
{
    public partial class AddImageBoxForm : AddFormBase, IDictSubject<byte[]>
    {
        private Image loadedImage;
        private PageImageBox pageImageBox;

        public AddImageBoxForm()
        {
            InitializeComponent();
        }

        public override PageControl CreatedControl
        {
            get
            {
                return pageImageBox;
            }
            set
            {
                pageImageBox = (PageImageBox)value;
            }
        }

        private void AddImageBoxForm_Load(object sender, EventArgs e)
        {
            pageImageBox = new PageImageBox();
            Dictionary = new SDictionary<byte[]>();
            textBoxName.Text = EditorForm.GenerateName(pageImageBox.NamingPrefix);
            comboBoxLanguage.Items.AddRange(EditorForm.SupportedLanguages);
            comboBoxLanguage.SelectedIndex = 0;
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("İsim girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!EditorForm.IsElementNameAvailable(textBoxName.Text.Trim()))
            {
                MessageBox.Show("Bu isim zaten var!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (loadedImage == null)
            {
                MessageBox.Show("Resim seçmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int width = -1, height = -1;

            if (!int.TryParse(textBoxWidth.Text, out width) || width <= 0)
            {
                MessageBox.Show("Genişlik değeri pozitif bir tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!int.TryParse(textBoxHeight.Text, out height) || height <= 0)
            {
                MessageBox.Show("Yükseklik değeri pozitif bir tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            string selectedLanguage = comboBoxLanguage.SelectedItem as string;
            
            pageImageBox.Name = textBoxName.Text.Trim();
            Dictionary[selectedLanguage] = ImageWorker.GetByteArray(panelImage.BackgroundImage);
            pageImageBox.Dictionary = Dictionary;
            //pageImageBox.Image = loadedImage;
            pageImageBox.Size = new Size(width, height);
            pageImageBox.ScrollableSize = pageImageBox.Size;
            pageImageBox.Visible = true;
            pageImageBox.UpdateInnerControl();
            DialogResult = DialogResult.OK;
        }

        private void buttonImage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                loadedImage = Image.FromStream(openFileDialog1.OpenFile());
                panelImage.BackgroundImage = loadedImage;
                textBoxWidth.Text = loadedImage.Width.ToString();
                textBoxHeight.Text = loadedImage.Height.ToString();
            }
        }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            SDictionary<byte[]> dict = Dictionary;
           
            panelImage.BackgroundImage = ImageWorker.GetImage(
                changeSubject(comboBoxLanguage.SelectedItem as string,
                    comboBoxLanguage.SelectedIndex, 
                    ImageWorker.GetByteArray(panelImage.BackgroundImage), 
                    ref dict));
            Dictionary = dict;
        }

        public SDictionary<byte[]> Dictionary { get; set; }
    }
}
